﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MsForgeReboot.Framework.Entities;
using MsForgeReboot.Framework.TownCrierChannels;
using MsForgeReboot.Framework.TownCrierChannelTools;
using RestSharp;

namespace MsForgeReboot.Framework.TownCrierChannels {
    public class EventbriteChannel : RestChannelBase, IChannel {
        
        // TODO finish this channel

        private string _appkey;
        private string _userkey;
        private string _baseurl;
        private string _resource;

        public EventbriteChannel() : this("YTk5OTFkYjJhYzZl", "129507314010072586377 ") { }

        public EventbriteChannel(string appkey, string userkey) {
            _baseurl = "https://https://www.eventbrite.com";
            _resource = "/xml/event_new";
            _appkey = appkey;
            _userkey = userkey;
        }

        #region IChannel Members

        public override DeliveryResult DeliverEvent(Event source) {
            //throw new NotImplementedException();
            EventBriteAuthenticator auth = new EventBriteAuthenticator(_appkey,_userkey);
            Parameter[] ps = GetParameterArray(source);


            DeliveryResult result = base.Get(_baseurl, _resource, auth, ps);

            result.DeliveryChannel = "EventBrite Channel";
            return result;

        }

        private Parameter[] GetParameterArray(Event source) {
            //throw new NotImplementedException();
            string tz = "GMT" + source.Begin.Value.ToString("z", System.Threading.Thread.CurrentThread.CurrentCulture);
            Parameter[] result = new Parameter[] {
                new Parameter { Name="title", Value=source.Title, Type = ParameterType.GetOrPost},
                new Parameter{Name="description",Value=source.Body,Type=ParameterType.GetOrPost},
                new Parameter{Name="start_date",Value=source.Begin.Value.ToString("U"),Type=ParameterType.GetOrPost},
                new Parameter{Name="end_date",Value=source.End.Value.ToString("U"),Type=ParameterType.GetOrPost},
                new Parameter{Name="timezone",Value=tz,Type=ParameterType.GetOrPost}
            };
            return result;

        }

        #endregion
    }
}
